package com.meilele.biz.zhengchao;


import com.meilele.dao.zhengchao.FourArticleMapper;
import com.meilele.dao.zhengchao.FourContentMapper;
import com.meilele.entity.zhengchao.FourArticle;
import com.meilele.entity.zhengchao.FourArticleVO;
import com.meilele.entity.zhengchao.FourContent;
import com.meilele.entity.zhengchao.HotArticle;
import com.meilele.util.zhengchao.Contants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleBiz {
    @Resource
    private FourArticleMapper dao;
    @Resource
    private FourContentMapper cdao;
    public List<FourArticle> findArticleByType(int bigType,int pageNum) {
        int  number = (pageNum-1) * Contants.PAGE_NUMBER;
        return dao.findArticleByType(bigType,number,Contants.PAGE_NUMBER);
    }


    public List<FourArticle> findAll(int page) {
        int  number = (page-1) * Contants.PAGE_NUMBER;
        return dao.findAll(number,Contants.PAGE_NUMBER);
    }
    public int getCount(int id) {
        return dao.getCount(id);

    }
    public int getCount1() {
        return dao.getCount1();

    }

    public void save(FourArticle article) {
        cdao.insertSelective(article.getFourContent());
        FourArticleVO ao=new FourArticleVO();
        ao.setArticleHead(article.getArticleHead());
        ao.setArticleImage(article.getArticleImage());
        ao.setArticleSmallTitle(article.getArticleSmallTitle());
        ao.setArticleTitle(article.getArticleTitle());
        ao.setArticleType(article.getFourType().getTypeId());
        ao.setArticleUploadData(LocalDateTime.now().toString());
        ao.setContentId(article.getFourContent().getContentId());
        System.out.println(article.getFourContent().getContentId());
        dao.insert(ao);
    }

    public FourArticle findById(int id) {
        return dao.selectByPrimaryKey(id);
    }

    public void updateById(FourArticle article) {
        cdao.updateByPrimaryKeySelective(article.getFourContent());
        FourArticleVO ao=new FourArticleVO();
        ao.setArticleHead(article.getArticleHead());
        ao.setArticleImage(article.getArticleImage());
        ao.setArticleSmallTitle(article.getArticleSmallTitle());
        ao.setArticleTitle(article.getArticleTitle());
        ao.setArticleType(article.getFourType().getTypeId());
        ao.setArticleUploadData(LocalDateTime.now().toString());
        ao.setContentId(article.getFourContent().getContentId());
        ao.setArticleId(article.getArticleId());
        dao.updateByPrimaryKeySelective(ao);
    }

    public void deleteById(int id) {
        FourArticle article=dao.selectByPrimaryKey(id);
        dao.deleteByPrimaryKey(id);
        cdao.deleteByPrimaryKey(article.getFourContent().getContentId());
    }

    public FourContent findContentById(int id) {
        FourArticle article=dao.selectByPrimaryKey(id);
        return cdao.selectByPrimaryKey(article.getFourContent().getContentId());
    }

    public List<HotArticle> orderByReader() {
        List<FourArticle> list=dao.orderByReader();
        HotArticle hot=null;
        List<HotArticle> ls=new ArrayList();
        int i=1;
        for(FourArticle aa:list){
            hot=new HotArticle();
            hot.setId(i);
            hot.setFourArticle(aa);
            ls.add(hot);
            i++;
        }
        return ls;
    }

    public List<FourArticle> orderByTime(int i) {
        int pageNum= Contants.PAGE_NUMBER * (i-1);
        return dao.orderByTime(pageNum,Contants.PAGE_NUMBER);
    }

    public void updateReadById(int id, Integer bb) {
        dao.updateReadById(id,bb);
    }
}
